let app = getApp();
const Promise = require("/plugins/es6-promise");
import omsRequest from "/utils/omsRequest";
import { getCrmHomeData } from "/api/index";
import { getDistributable } from "/api/distributeAchievement";
import { showToast } from "/utils/common";
Page({
  data: {
    permissions: [], //权限树key值
    userInfo: null,
    systemInfo: null,
    crmHomeData: {}, // crm首页数据
    currentIndex: 0, // 轮播item 当前值
    saleMonthTargetTab: [],
    distributableList: [] // 是否被分配过 
  },
  onLoad() {
    // console.log(dd.getMenuButtonBoundingClientRect())
    //首页调用 app.js 中登录方法-页面加载。。。
    dd.showLoading({
      content: '加载中...'
    });
    app.getAuthCodeMethod().then(() => {
      this.getCrmHomeData().then(res => {
        res.merchant.rate = Math.ceil((res.merchant.finish / res.merchant.total) * 10000) / 100
        res.deposit.rate = Math.ceil((res.deposit.finish / res.deposit.total) * 10000) / 100
        res.visit.rate = Math.ceil((res.visit.finish / res.visit.total) * 10000) / 100
        this.setData({ //获取登录成功后的 信息 和crm首页数据
          systemInfo: app.globalData.systemInfo,
          permissions: app.globalData.permissions,
          userInfo: app.globalData.userInfo,
          crmHomeData: res
        });
        // 按城市分配 总监  按人员分配 主管---获取可分配数量
        omsRequest({
          url: getDistributable
        }).then((res) => {
          if (res.success) { // 获取可分配数量// 初始未分配--可分配数量无--空数组 // 总监分配过后-再次查询可分配数量-数组有值
            let arr = [
              {
                targetType: 'DEPOSIT',
                rotateValue: -30 // 仪表盘指针初始度数
              },
              {
                targetType: 'MERCHANT',
                rotateValue: -30 // 仪表盘指针初始度数
              },
              {
                targetType: 'VISIT',
                rotateValue: -30 // 仪表盘指针初始度数
              }
            ]
            if (res.voList.length) { // 分配过后--合并接口返回值
              arr.forEach((item) => {
                res.voList.forEach((val) => {
                  if (item.targetType === val.targetType) {
                    Object.assign(item, val)
                  }
                })
              })
            }

            this.setData({
              distributableList: res.voList,
              saleMonthTargetTab: arr
            })
            this.switchSwiperChange(this.data.currentIndex)
            dd.hideLoading()
          }
        }).catch(() => {
          dd.hideLoading()
        })

      }).catch(() => {
        dd.hideLoading()
      })
    }).catch(() => {
      dd.hideLoading()
    })
  },
  handSwiperChange(e) {
    this.switchSwiperChange(e.detail.current)
    this.setData({
      currentIndex: e.detail.current,
    })
  },
  switchSwiperChange(value) {
    switch (value) {
      case 0:
        this.data.saleMonthTargetTab[0].rotateValue = (this.data.crmHomeData.deposit.finish / this.data.crmHomeData.deposit.total) * 231 - 24;
        break;
      case 1:
        this.data.saleMonthTargetTab[1].rotateValue = (this.data.crmHomeData.merchant.finish / this.data.crmHomeData.merchant.total) * 231 - 24;
      case 2:
        this.data.saleMonthTargetTab[2].rotateValue = (this.data.crmHomeData.visit.finish / this.data.crmHomeData.visit.total) * 231 - 24;
      default:
        break;
    }
    this.setData({
      saleMonthTargetTab: this.data.saleMonthTargetTab
    })
  },
  getCrmHomeData() {
    return new Promise((resolve, reject) => {
      omsRequest({
        url: getCrmHomeData
      }).then((res) => {
        resolve(res.vo)
      }).catch((err) => {
        reject(err)
      })

    })
  },
  goPrivateCustomers() {
    dd.navigateTo({
      url: '/pages/privateCustomers/private-customers-list/private-customers-list'
    })
  },
  goSignedMerchant() {
    dd.navigateTo({
      url: '/pages/signedMerchant/signedMerchantList/signedMerchantList'
    })
  },
  jumpToClock() {
    dd.navigateTo({
      url: '/pages/visitClock/punckClock/punckClock'
    })
  },
  goDistributeAchievement(e) {
    if (app.globalData.permissions.includes(467)) { // 按城市分配业绩-总监-页面组件
      dd.reLaunch({
        url: `/pages/distributeAchievementChief/distributeAchievementChief?saleMonthTargetTab=${JSON.stringify(this.data.saleMonthTargetTab)}&currentTabIndex=${e.currentTarget.dataset.index}`
      })
    }
    if (app.globalData.permissions.includes(468)) {// 按人员分配业绩----主管和-小组长分配--且只能分配收款金额--页面组件---
      // console.log(this.data.distributableList.filter((item) => item.targetType === 'DEPOSIT'))
      if (this.data.distributableList.filter((item) => item.targetType === 'DEPOSIT').length) {
        dd.reLaunch({
          url: `/pages/distributeAchievementDirector/distributeAchievementDirector?saleMonthTargetItem=${JSON.stringify(this.data.saleMonthTargetTab.find(item => item.targetType === 'DEPOSIT'))}`
        })
      } else {
        showToast({
          msg: '不存在待分配业绩'
        })
      }
    }
  },
  // goExportDataReport() {
  //   dd.navigateTo({
  //     url: '/pages/exportDataReport/exportDataReport'
  //   })
  // }
});
